//
//  KFLoginVC.m
//  KafeeSale
//
//  Created by Liboom on 2025/6/12.
//

#import "KFLoginVC.h"
#import "NewTabbarVC.h"
@interface KFLoginVC ()
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *pswTF;
@property (weak, nonatomic) IBOutlet UIImageView *picView;
@property(nonatomic, assign) NSInteger viewCount;
@end

@implementation KFLoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.myTitle = @"登录";
    self.view.backgroundColor = UnderLineColor;
    [self.picView addTarget:self action:@selector(picViewClick)];
}
-(void)picViewClick{
    self.viewCount ++;
    NSString * count = [NSString stringWithFormat:@"%ld",self.viewCount];
    XHQHUDText(count);
    if (self.viewCount == 5) {
        self.viewCount = 0;
        NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
        if ([[def objectForKey:@"ipDomain"] isEqualToString:@"http://43.138.162.50:1300"] ) {
            XHQHUDSuccessText(@"切换到测试ip成功");
            [def setObject:@"http://121.43.170.185:1300" forKey:@"ipDomain"];
            [def setObject:@"http://121.43.170.185:1300/uploads" forKey:@"ipOssDomain"];
        }else{
            XHQHUDSuccessText(@"切换到正式ip成功");
            [def setObject:@"http://43.138.162.50:1300" forKey:@"ipDomain"];
            [def setObject:@"http://43.138.162.50:1300/uploads" forKey:@"ipOssDomain"];
        }
    }
}
- (IBAction)loginClick:(id)sender {
    if (self.phoneTF.text.length ==  0) {
        XHQHUDErrorText(@"请输入手机号");
        return;
    }
    if(self.pswTF.text.length == 0){
        XHQHUDErrorText(@"请输入密码");
        return;
    }
    NSDictionary * dic = @{@"phone":self.phoneTF.text,@"password":self.pswTF.text};
    XHQHUDInView(self.view);
    [PGNetworkHelper POST:[WorkUrl returnURL:Interface_For_Login] parameters:dic cache:NO responseCache:nil success:^(id responseObject) {
        XHQHUDHide();
        if ([responseObject[@"code"] integerValue]==200) {
            NSUserDefaults*def =[NSUserDefaults standardUserDefaults];
            NSDictionary * data = (NSDictionary*) responseObject[@"data"];
            
            NSMutableDictionary *sanitizedDict = [data mutableCopy];
            [data enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
                if ([obj isKindOfClass:[NSNull class]]) {
                    [sanitizedDict removeObjectForKey:key]; // 移除null键值对
                }
            }];

            [[NSUserDefaults standardUserDefaults] setObject:sanitizedDict forKey:@"userInfo"];
            [def setObject:sanitizedDict forKey:@"userInfo"];
            [UIApplication sharedApplication].delegate.window.rootViewController = [NewTabbarVC new];
        }else{
            XHQHUDErrorText(responseObject[@"msg"]);
        }
    } failure:^(id error) {
        XHQHUDHide();
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
